package online.westonline.com.westonline.adapter.constacts;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.easy.west.westclient.api.department.bean.DepartMentBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.event.ListClickEvent;
import online.westonline.com.westonline.wedgit.DownImgView;

/**
 * 二级部门适配器
 * Created by xiatao on 2018/8/22.
 */

public class ChildDepartMentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<DepartMentBean> departMentBeanList = new ArrayList<>();
    private onItemClickListener itemClickListener;

    public void setDepartMentBeanList(List<DepartMentBean> departMentBeanList) {
        this.departMentBeanList = departMentBeanList;
    }

    public void setItemClickListener(onItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.department_dialog_item, parent, false);
        return new ChildDialogDepartMentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ChildDialogDepartMentViewHolder) {
            ChildDialogDepartMentViewHolder dialogDepartMentViewHolder = (ChildDialogDepartMentViewHolder) holder;
            final DepartMentBean departMentBean = departMentBeanList.get(position);
            dialogDepartMentViewHolder.tvDepartmentTitle.setText(departMentBean.getName());
            if (departMentBean.getFid()==1)
            {
                dialogDepartMentViewHolder.tvState.setText("机关");
            }
            else
            {
                dialogDepartMentViewHolder.tvState.setText("部门");
            }
            dialogDepartMentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new ListClickEvent(departMentBean));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return departMentBeanList == null ? 0 : departMentBeanList.size();
    }

    /***
     *viewholder
     */
    class ChildDialogDepartMentViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_department_title)
        TextView tvDepartmentTitle;
        @BindView(R.id.tv_state)
        TextView tvState;


        public ChildDialogDepartMentViewHolder(final View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new ListClickEvent(departMentBeanList.get(getLayoutPosition())));
                    if (null != itemClickListener) {
                        itemClickListener.onClick(getLayoutPosition(), departMentBeanList.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    public interface onItemClickListener {
        void onClick(int pos, DepartMentBean departMentBean);
    }
}
